package org.smart4j.chapter1.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.chapter1.annotation.Action;
import org.smart4j.chapter1.annotation.Controller;
import org.smart4j.chapter1.bean.Param;
import org.smart4j.chapter1.bean.View;
import org.smart4j.chapter1.plugin.security.exception.AuthcException;
import org.smart4j.chapter1.plugin.security.SecurityHelper;

/**
 * @ClassName:
 * @Description: 处理系统请求
 * @Author: LynnZou
 * @Date: 2017/5/31
 */
@Controller
public class SystemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemController.class);

    /**
     * 进入首页面
     * @return
     */
    @Action("get:/")
    public View index(){
        return new View("index.jsp");
    }


    /**
     * 进入登陆页面
     * @return
     */
    @Action("get:/login")
    public View login(){
        return new View("login.jsp");
    }

    /**
     * 提交登陆表单
     * @param param
     * @return
     */
    @Action("post:/login")
    public View loginSubmit(Param param){
        String usernaem = param.getString("username");
        String password = param.getString("password");

        try {
            SecurityHelper.login(usernaem,password);
        } catch (AuthcException e) {
            LOGGER.error("login faulure",e);
            return new View("/login");
        }
        return new View("/customer");
    }


    /**
     * 提交注销请求
     */
    @Action("get:/logout")
    public View logout(){
        SecurityHelper.logout();
        return new View("/");
    }
}
